package pro.softzhang.algo.lc0;


import pro.softzhang.algo.flag.knowledge.KDoublePointer;
import pro.softzhang.algo.flag.x.XOptimal;

/**
 * 11. https://leetcode.cn/problems/container-with-most-water/description/
 */
public class LC11_ContainerWithMostWater {
    public static void main(String[] args) {

    }

    @KDoublePointer
    @XOptimal
    static
    class Solution {
        public int maxArea(int[] height) {
            int p1 = 0, p2 = height.length - 1, maxArea = 0;
            while (p1 < p2) {
                maxArea = height[p1] < height[p2] ?
                        Math.max(maxArea, (p2 - p1) * height[p1++]) :
                        Math.max(maxArea, (p2 - p1) * height[p2--]);
            }
            return maxArea;
        }
    }

    static
    class Solution1 {
        public int maxArea(int[] height) {
            int max = 0, l = 0, r = height.length - 1;
            while (l < r) {
                int area = (r - l) * Math.min(height[l], height[r]);
                max = Math.max(max, area);
                if (height[l] < height[r]) {
                    l++;
                } else {
                    r--;
                }
            }
            return max;
        }
    }
}
